import time
from playwright.sync_api import sync_playwright
import pytest
import json
from package_for_test.utl.save_dom import save_dom


def scrape_and_save(url, output_prefix="page"):
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False)
        context = browser.new_context()
        page = context.new_page()
        page.goto(url)
        page.wait_for_load_state("networkidle")  # 等待页面完全加载

        # === 1. 获取完整 HTML 内容（DOM 树）===
        page_content = page.content()
        page_evaluate = page.evaluate("document.documentElement.outerHTML")
        # save_dom(page_evaluate=data, page_content=html_content)
        # print(html_content)
        with open(f"{output_prefix}.json", "w", encoding="utf-8") as f:
            json.dump(page_evaluate, f, ensure_ascii=False, indent=2)
        print(f"✅ JSON 已保存到: {output_prefix}.json")

        with open(f"{output_prefix}.html", "w", encoding="utf-8") as f:
            f.write(page_content)
        print(f"✅ HTML 已保存到: {output_prefix}.html")
        print(page_evaluate)
        browser.close()


if __name__ == '__main__':
    url = "https://team.pescms.com/?g=Team&m=Login&a=index&back_url=Lw=="
    scrape_and_save(url)
